package com.tbit.main.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.tbit.main.constant.AccountDayStatConstant;
import com.tbit.main.constant.DaoParamConstant;
import com.tbit.main.dao.stat.AccountDayStatDao;
import com.tbit.main.dao.stat.PowerDayStaDao;
import com.tbit.main.pojo.AccountDayStat;
import com.tbit.main.pojo.PowerDaySta;
import com.tbit.main.service.PowerDayStaService;
import com.tbit.main.util.DateTimeUtil;
import com.tbit.main.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author LMY
 * @create 2021-01-12 18:57
 * 换电统计
 */
@Service("powerDayStaService")
public class PowerDayStaServiceImpl implements PowerDayStaService {


    @Autowired
    private PowerDayStaDao powerDayStaDao;
    @Autowired
    private AccountDayStatDao accountDayStatDao;

    @Override
    public List<PowerDaySta> getByAccountIdDateDay(List<Integer> accountIds, List<Integer> regionId, String startDate, String endDate) {
        Map<String, Object> map = new HashMap<>();
        map.put(DaoParamConstant.accountId, StringUtil.getListString(accountIds));
        map.put("regionId", StringUtil.getListString(regionId));
        map.put("startDate", DateTimeUtil.getYYMMDDInteger(startDate));
        map.put("endDate", DateTimeUtil.getYYMMDDInteger(endDate));
        map.put("dataType", AccountDayStatConstant.NUMBER_OF_POWER_CHANGES);
        List<PowerDaySta> powerDayStaList = new LinkedList<>();
        List<PowerDaySta> moveDayStaLinkedList = new LinkedList<>();
        if (accountIds != null && accountIds.size() > 0) {
            //powerDayStaList = powerDayStaDao.getByAccountIdDateDay(map);

            List<AccountDayStat> accountDayStats = accountDayStatDao.getByAccountIdDateDay(map);
            moveDayStaLinkedList = accountDayStats.stream().map(l -> new PowerDaySta(l.getAccountId(), null, l.getSumTotal().intValue(), DateTimeUtil.getYY_MM_DD(l.getDataDate().toString()), l.getAddTime(), l.getCntTotal(), l.getDateWeek(), l.getDateMonth(), (double) l.getCntTotal())).collect(Collectors.toList());
        }
        if (CollUtil.isNotEmpty(regionId)) {
            List<PowerDaySta> powerDayStas = powerDayStaDao.getByRegionIdDateDay(map);
            powerDayStaList.addAll(powerDayStas);
        }

        return moveDayStaLinkedList;
    }

    @Override
    public List<PowerDaySta> getByAccountIdDateWeek(List<Integer> accountIds, List<Integer> regionId, String startDate, String endDate) {
        Map<String, Object> map = new HashMap<>();
        map.put(DaoParamConstant.accountId, StringUtil.getListString(accountIds));
        map.put("regionId", StringUtil.getListString(regionId));
        map.put("startDate", DateTimeUtil.getYYMMDDInteger(startDate));
        map.put("endDate", DateTimeUtil.getYYMMDDInteger(endDate));
        map.put("dataType", AccountDayStatConstant.NUMBER_OF_POWER_CHANGES);
        List<PowerDaySta> powerDayStaList = new LinkedList<>();
        List<PowerDaySta> moveDayStaLinkedList = new LinkedList<>();
        if (accountIds != null && accountIds.size() > 0) {
            //powerDayStaList = powerDayStaDao.getByAccountIdDateWeek(map);

            List<AccountDayStat> accountDayStats = accountDayStatDao.getByAccountIdDateWeek(map);
            moveDayStaLinkedList = accountDayStats.stream().map(l -> new PowerDaySta(l.getAccountId(), null, l.getSumTotal().intValue(), DateTimeUtil.getYY_MM_DD(l.getDataDate().toString()), l.getAddTime(), l.getCntTotal(), l.getDateWeek(), l.getDateMonth(), new BigDecimal((float) Integer.parseInt(l.getCntTotal().toString()) / 7).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue())).collect(Collectors.toList());
        }
        if (CollUtil.isNotEmpty(regionId)) {
            List<PowerDaySta> powerDayStas = powerDayStaDao.getByRegionIdDateWeek(map);
            powerDayStaList.addAll(powerDayStas);
        }
        return moveDayStaLinkedList;
    }

    @Override
    public List<PowerDaySta> getByAccountIdDateMonth(List<Integer> accountIds, List<Integer> regionId, String startDate, String endDate) {
        Map<String, Object> map = new HashMap<>();
        map.put(DaoParamConstant.accountId, StringUtil.getListString(accountIds));
        map.put("regionId", StringUtil.getListString(regionId));
        map.put("startDate", DateTimeUtil.getYYMMDDInteger(startDate));
        map.put("endDate", DateTimeUtil.getYYMMDDInteger(endDate));
        map.put("dataType", AccountDayStatConstant.NUMBER_OF_POWER_CHANGES);
        List<PowerDaySta> powerDayStaList = new LinkedList<>();
        List<PowerDaySta> moveDayStaLinkedList = new LinkedList<>();
        if (accountIds != null && accountIds.size() > 0) {
            //powerDayStaList= powerDayStaDao.getByAccountIdDateMonth(map);

            List<AccountDayStat> accountDayStats = accountDayStatDao.getByAccountIdDateMonth(map);
            moveDayStaLinkedList = accountDayStats.stream().map(l -> new PowerDaySta(l.getAccountId(), null, l.getSumTotal().intValue(), DateTimeUtil.getYY_MM_DD(l.getDataDate().toString()), l.getAddTime(), l.getCntTotal(), l.getDateWeek(), l.getDateMonth(), new BigDecimal((float) Integer.parseInt(l.getCntTotal().toString()) / 30).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue())).collect(Collectors.toList());
        }

        if (CollUtil.isNotEmpty(regionId)) {
            List<PowerDaySta> regionIdDateDay = powerDayStaDao.getByRegionIdDateMonth(map);
            powerDayStaList.addAll(regionIdDateDay);
        }

        return moveDayStaLinkedList;
    }

    @Override
    public Integer getByAccountId(Integer accountId, String startDate, String endDate) {
        Map<String, Object> map = new HashMap<>();
        map.put(DaoParamConstant.accountId, accountId);
        map.put("startDate", startDate);
        map.put("endDate", endDate);
        return powerDayStaDao.getByAccountId(map);
    }

    @Override
    public Integer getByRegionId(Integer regionId, String startDate, String endDate) {
        Map<String, Object> map = new HashMap<>();
        map.put("regionId", regionId);
        map.put("startDate", startDate);
        map.put("endDate", endDate);
        return powerDayStaDao.getByRegionId(map);
    }

}
